package com.itheima.sh.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决请求乱码
        request.setCharacterEncoding("UTF-8");
        //2.获取页面中请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        //3.从session中获取验证码内容(就是图片的内容)
        String checkcode = (String) request.getSession().getAttribute("checkcode");
        response.setContentType("text/html;charset=utf-8");
        //4.判断输入框输入的验证码是否和session中即验证码图片的内容是否相等
        if (checkcode.equalsIgnoreCase(code)) {
            //5.相等：验证码校验通过，实际开发中将用户名和密码传递到数据库查询，输出给前端校验通过
            response.getWriter().print("验证码校验通过");
        } else {
            //6.不相等:验证码校验未通过，告知浏览器验证码有误
            response.getWriter().print("验证码校验失败");
        }
    }
}